package com.problemproject.dome.encryption;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.problemproject.dome.R;

import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

/**
 * DataEncryptionActivity
 * 数据加密方式
 * @author huo
 * @date 2022/4/11 下午 05:21
 */
public class DataEncryptionActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText edData;
    private Button dataRsa;
    private Button dataAes;
    private Button dataSimpleness;

    private TextView textViewRsa;
    private TextView textViewAes;
    private TextView textViewSimpleness;
    private Button dataRsaTwo;
    private TextView textViewRsaTwo;
    private   Map<String, String> stringStringMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data_encryption);


        edData = (EditText) findViewById(R.id.ed_data);
        dataRsa = (Button) findViewById(R.id.data_rsa);
        dataAes = (Button) findViewById(R.id.data_aes);
        dataSimpleness = (Button) findViewById(R.id.data_simpleness);

        textViewRsa = (TextView) findViewById(R.id.textView_rsa);
        textViewAes = (TextView) findViewById(R.id.textView_aes);
        textViewSimpleness = (TextView) findViewById(R.id.textView_simpleness);



        dataRsaTwo = (Button) findViewById(R.id.data_rsa_two);
        textViewRsaTwo = (TextView) findViewById(R.id.textView_rsa_two);


        dataRsa.setOnClickListener(this);
        dataAes.setOnClickListener(this);
        dataSimpleness.setOnClickListener(this);
        dataRsaTwo.setOnClickListener(this);

        try {
            stringStringMap= RSAUtils.generateKeyPair();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.data_rsa:
                try {
                   //加密
                    String publicKey = stringStringMap.get("publicKey");
                    String aesStr = edData.getText().toString();
                    aesStr = RSAUtils.encrypt(aesStr, publicKey);
                    textViewRsa.setText(aesStr);

                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
            case R.id.data_rsa_two:
                try {
                    //解密
                    String privateKey = stringStringMap.get("privateKey");
                    String string = textViewRsa.getText().toString();
                    String jieMiStr = RSAUtils.decrypt(string, privateKey);
                    textViewRsaTwo.setText(jieMiStr);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.data_aes:
                break;
            case R.id.data_encryption:
                break;
        }
    }
}